package com.isunland.managesystem.utils;

import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.SuccessMessage;
import com.loopj.android.http.AsyncHttpResponseHandler;
import org.apache.http.Header;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/7/20 17:45
 */

@SuppressWarnings({ "deprecation", "unused" }) public class SimpleUploadHandler
    extends AsyncHttpResponseHandler {

  @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) {
    SuccessMessage message;
    try {
      message = new Gson().fromJson(new String(response), SuccessMessage.class);
      String result = message.getResult();
      LogUtil.i(new String(response));
      if (SuccessMessage.SUCCESS.equals(result)) {

        ToastUtil.show(R.string.fileUploadSuccess);
        onUploadSuccess(message.getMessage());
      }
      if (SuccessMessage.FAILURE.equals(result)) {
        onUploadFailure();
        ToastUtil.show(R.string.fileUploadFailure);
      }
    } catch (Exception e) {
      LogUtil.e("fileUploadError", e);
      // Toast.makeText(mContext, R.string.wrong_data,
      // Toast.LENGTH_SHORT).show();
      ToastUtil.show(R.string.fileUploadFailure);
    }
  }

  @SuppressWarnings("WeakerAccess") public void onUploadFailure() {

  }

  @SuppressWarnings("WeakerAccess")
  public void onUploadSuccess(@SuppressWarnings("UnusedParameters") String path) {

  }

  @Override
  public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
    // LogUtil.e(TAG, "statusecode=" + statusCode + ",errorResponse="
    // + new String(errorResponse), e);
    ToastUtil.show(R.string.fileUploadFailure);
    onUploadError(e);
  }

  @SuppressWarnings("WeakerAccess") public void onUploadError(Throwable e) {

  }

  @Override public void onRetry(int retryNo) {
    // called when request is retried
  }
}
